<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">
            <?php include "../public/substt.php";?>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>项目信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=contract_bian','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=contract_bian','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>主合同信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>主合同</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="caiid" class="layui-input caiid" onclick="choose_contract_cai()">
                            <div class="choose_div" id="caiid" key="1"></div>
                            <span class="choose_one" onclick="choose_contract_cai()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">合同类型</label>
                        <div class="layui-input-block">
                            <input type="hidden" id="caitype">
                            <input type="text" id="cai_caitype"  placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">合同金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="cai_money" readonly autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">税率(%)</label>
                        <div class="layui-input-block">
                            <input type="text" id="cai_rate" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>变更信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">变更主题</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" id="title" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>本次变更金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="bianfee" id="bianfee" placeholder="请输入" readonly autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>变更后合同金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="houfee" id="houfee" placeholder="请输入" readonly autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">变更签订日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="dt" id="dt" placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">经手人</label>
                        <div class="layui-input-block">
                            <input type="text" name="baner" id="baner" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>



            <div class="layui-form-item height_auto" id="caiDiv" budgetid="0">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>变更明细</span></div>
                        <div>
                            <button type="button" id="list_budget" onclick="choose_engin_budget_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn layui-hide">从预算明细新增</button>
                            <button type="button" onclick="choose_contract_cai_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从合同明细变更</button>
                            <button type="button" id="list_add" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row layui-hide">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th class="layui-hide">预算项</th>
                            <th>名称及规格</th>
                            <th>单位</th>
                            <th>数量</th>
                            <th>单价(元)</th>
                            <th>金额(元)</th>
                            <th><text>*</text>变更后数量</th>
                            <th><text>*</text>变更后单价(元)</th>
                            <th>变更后金额(元)</th>
                            <th>变更原因</th>
                            <th>来源</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
            <?php include '../public/flowqita.php'?>
            <?php include '../public/upinfo.php'?>
        </div>
    </div>
</form>
<script>
    var parent_caiid = getQueryVariable('caiid') || 0;

    $("#commentForm").validate({
        rules: {
            title: {required: true},
        },
        messages:{
            title: {required: "请输入"},
        }
    });

    function choose_contract_cai(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目");return false;
        }
        openPage('../public/choose_contract_cai.php?dom=caiid&page=contract_bian&enginid='+enginid,'80%','80%')
    }

    function choose_engin_budget_list(obj){
        var budgetid = $("#caiDiv").attr('budgetid');
        var caitype = $("#caitype").val() || '';
        if(!caitype){
            facade.error("请先选择主合同");return false;
        }
        openPage('../public/choose_engin_budget_list.php?choose_type=checkbox&page=contract_bian&budgetid='+budgetid+'&budtype='+caitype,'80%','80%');
    }

    function choose_contract_cai_list(obj){
        var caiid = get_choose_val('caiid');
        if(!caiid){
            facade.error("请先选择合同名称");return false;
        }
        openPage('../public/choose_contract_cai_list.php?page=contract_bian&caiid='+caiid,'80%','80%');
    }


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_flowdata_add_contract_bian, data:{caiid: parent_caiid}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                chu_flow_qian(data);

                var engininfo = res.data.engininfo;
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                    $("#caiDiv").attr('budgetid', engininfo.budgetid);
                }

                var caiinfo = res.data.caiinfo;
                if(caiinfo){
                    set_choose_btn('caiid',caiinfo.id,caiinfo.name);
                    $("#caitype").val(caiinfo.caitype);
                    $("#cai_caitype").val(caiinfo.caitype_name);
                    $("#cai_money").val(caiinfo.money);
                    $("#cai_rate").val(caiinfo.rate);
                }

                $("#dt").val(data.dt);
                $("#baner").val(data.baner);





                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td>';
                listAddStr += '<input type="text" readonly bglistid="0" listid="0" class="layui-input listid disabled">';
                listAddStr += '</td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td><input type="text" class="layui-input unit"></td>';
                listAddStr += '<td><input type="number" class="layui-input num disabled" value="0" disabled></td>';
                listAddStr += '<td><input type="number" class="layui-input price disabled" value="0" disabled></td>';
                listAddStr += '<td><input type="number" class="layui-input money disabled" value="0" disabled></td>';
                listAddStr += '<td><input type="number" class="layui-input new_num"></td>';
                listAddStr += '<td><input type="number" class="layui-input new_price"></td>';
                listAddStr += '<td><input type="number" class="layui-input new_money disabled" value="" disabled></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '<td><input type="text" class="layui-input lai disabled" value="新增" disabled></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1);
                update_refresh_row_func()

                public_upload_file_load('upload_files', res.data.files);
                render_from();
            }
        });


        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');



            data.field.enginid = get_choose_val('enginid');
            data.field.caiid = get_choose_val('caiid');

            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();
                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }

                formValid('caiid');
                if(data.field.caiid == undefined || !data.field.caiid){
                    validRes = false;
                }
                if(!validRes){return false;}
            }


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var bglistid = $(this).find('.listid').attr('bglistid') || 0;
                    var listid = $(this).find('.listid').attr('listid') || 0;
                    var name = $(this).find('.name').val();
                    var unit = $(this).find('.unit').val();
                    var num = $(this).find('.num').val();
                    var price = $(this).find('.price').val();
                    var money = $(this).find('.money').val();
                    var new_num = $(this).find('.new_num').val();
                    var new_price = $(this).find('.new_price').val();
                    var new_money = $(this).find('.new_money').val();
                    var explain = $(this).find('.explain').val();
                    var lai = $(this).find('.lai').val();

                    var index = i + 1;

                    if(data.field.isturn > 0){
                        if(new_num == ''){
                            throw new Error('第'+index+'行，请输入变更后数量');
                        }
                        if(new_price == ''){
                            throw new Error('第'+index+'行，请输入变更后单价');
                        }

                        if(listid > 0){
                            var chongKey = check_chongfu(i, listid);
                            if(chongKey){
                                throw new Error('第'+chongKey+'行'+'与第'+index+'行，数据重复');
                            }
                        }else{
                            var chongKey = check_chongfu222(i, name, unit);
                            if(chongKey){
                                throw new Error('第'+chongKey+'行'+'与第'+index+'行，数据重复');
                            }
                        }
                    }


                    list.push({
                        bglistid:bglistid,
                        listid:listid,
                        name:name,
                        unit:unit,
                        num:num,
                        price:price,
                        money:money,
                        new_num:new_num,
                        new_price:new_price,
                        new_money:new_money,
                        explain:explain,
                        lai:lai,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;

            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            public_flow_submit(data.field, ask_flowdata_add_contract_bian, ask_flowdata_edit_contract_bian, $(this).attr('but'));
            return false;
        });

    });


    //检测重复
    function check_chongfu(index, listid){
        try {
            $('#list_list tr').each(function (i) {
                var cur_listid = $(this).find('.name').attr('listid');
                if(index != i){
                    if(cur_listid == listid){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }
        return 0;
    }

    //检测重复
    function check_chongfu222(index, name, unit){
        try {
            $('#list_list tr').each(function (i) {
                var cur_name = $(this).find('.name').val();
                var cur_unit = $(this).find('.unit').val();
                if(index != i){
                    if(name == cur_name && unit == cur_unit){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }


    $('#list_list').on('input propertychange','.new_num,.new_price',function () {
        jisuan_money_all();
    });
    function jisuan_money_all() {
        var bianfee = 0;
        $('#list_list tr').each(function (i) {
            var new_num = ($(this).find('.new_num').val() || 0) || 0;
            var new_price = ($(this).find('.new_price').val() || 0) || 0;

            var new_money = parseFloat(new_num)*parseFloat(new_price);
            $(this).find('.new_money').val(new_money.toFixed(2));

            var money = ($(this).find('.money').val() || 0) || 0;
            bianfee = parseFloat(bianfee) + parseFloat(new_money) - parseFloat(money);
        });
        $("#bianfee").val(parseValidNum(bianfee, 2));

        var cai_money = parseValidNum($("#cai_money").val()) || 0;
        var houfee = parseFloat(cai_money) + parseFloat(bianfee);
        $("#houfee").val(parseValidNum(houfee, 2));

        initParseMoney();
    }




    function set_list_layout() {
        var budgetid = $("#caiDiv").attr('budgetid');
        if(budgetid > 0){
            $("#caiDiv table tr th, #caiDiv table tr td").removeClass('layui-hide');
            $("#list_budget").removeClass('layui-hide');
            $("#list_add").addClass('layui-hide');
        }else{
            $("#caiDiv table tr th:nth-child(2)").addClass('layui-hide');
            $("#caiDiv table tr td:nth-child(2)").addClass('layui-hide');
            $("#list_budget").addClass('layui-hide');
            $("#list_add").removeClass('layui-hide');
        }
    }

    function update_refresh_row_func() {
        render_date();
        render_from();
        set_list_layout();
        jisuan_money_all();

        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }

</script>
</body>
</html>
